import logging
import os
import sys
from os.path import abspath, join, dirname

from dotenv import load_dotenv, find_dotenv
from loguru import logger

from app.core.logging import InterceptHandler

API_PREFIX = "/api"
VERSION = "0.0.0"
DEBUG = os.getenv('DEBUG', True)
PROJECT_NAME = "FastAPI 程序"

LOGGING_LEVEL = logging.DEBUG if DEBUG else logging.INFO
LOGGERS = ("uvicorn.asgi", "uvicorn.access")

logging.getLogger().handlers = [InterceptHandler()]
for logger_name in LOGGERS:
    logging_logger = logging.getLogger(logger_name)
    logging_logger.handlers = [InterceptHandler(level=LOGGING_LEVEL)]

logger.configure(handlers=[{"sink": sys.stderr, "level": LOGGING_LEVEL}])
system_variables = os.environ["USER"]

ROOT_DIR = abspath(join(dirname(abspath(__file__)), '../../'))

env_name = f'.{system_variables}.env'
if os.path.exists(os.path.join(os.path.dirname(__file__), env_name)):
    load_dotenv(find_dotenv(filename=env_name))
else:
    load_dotenv(find_dotenv(filename=".env"))

MYSQL_USER = os.getenv('MYSQL_USER', 'root')
MYSQL_PASSWORD = os.getenv('MYSQL_PASSWORD', 'asdfasdf')
MYSQL_HOST = os.getenv('MYSQL_HOST', '127.0.0.1')
MYSQL_PORT = os.getenv('MYSQL_PORT', 3306)
MYSQL_DB = os.getenv('MYSQL_DB', 'designer')

SECRET_KEY = "b8f1d07e2c3282f1b8123b7d89cf3b0e6bc541b3a58c5dbb99d82bc481d7531b"
ALGORITHM = "HS256"
ACCESS_TOKEN_EXPIRE_MINUTES = 60 * 24 * 7

REDIS_HOST = os.getenv('REDIS_HOST', '127.0.0.1')
REDIS_PORT = os.getenv('REDIS_PORT', 6379)

MEMCACHED_HOST = os.getenv("MEMCACHED_HOST", '127.0.0.1')
ELASTICSEARCH_HOSTS = os.getenv("ELASTICSEARCH_HOSTS", 'http://127.0.0.1:9200')

TORTOISE_ORM = {
    "connections": {"default": f"mysql://{MYSQL_USER}:{MYSQL_PASSWORD}@{MYSQL_HOST}:3306/{MYSQL_DB}"},
    "apps": {
        "models": {
            "models": ["app.model.mysql", "aerich.models"],
            "default_connection": "default",
        },
    },
}

CORS_ORIGINS = ["39.107.13.161:8000"]
CORS_ALLOW_CREDENTIALS = True
CORS_ALLOW_METHODS = ["*"]
CORS_ALLOW_HEADERS = ["*"]

QINIU_ACCESS_KEY = 'Sw9cBvAZ42ZAsfkKNFbhIvOEncnEBnJcs4y94-OA'
QINIU_SECRET_KEY = 'zP3KgaP3OunQg96flQZ9AeKGhxyk20iYmcTPilkk'

QINIU_MEDIA_HOST = 'http://media.zc0901.com'

DEFAULT_AVATAR = 'http://static.zc0901.com/dci/default_avatar.png'

SMS_SECRET_ID = 'AKIDRgaFerBLp9D1ZhAIuMbaJo3CAuNf3V1H'
SMS_SECRET_KEY = '99tMO9hCRPFsERRCgRwMWQ7YnlT6i381'
SMS_APP_ID = '1400354636'
SMS_SIGN = 'DCI瓷砖'
SMS_TEMPLATE = {
    "code": {  # 验证码：{1}（{2}分钟内有效），请勿向他人泄露！
        "id": "585163",
        "len": 2
    }
}

LIMIT = 20
